package com.example.uiview_025;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;

public class ProgressDialogActivity extends Activity {

	private Handler handler = new Handler(new Handler.Callback() {
		@Override
		public boolean handleMessage(Message arg0) {
			ProgressDialog dialog = (ProgressDialog) arg0.obj;
			dialog.cancel();
			return false;
		}
	});

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_progress_dialog);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.progress_dialog, menu);
		return true;
	}

	public void test(View view) {
		final ProgressDialog dialog = new ProgressDialog(this);
		dialog.setTitle("test");
		dialog.setMessage("downloading");
		dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		dialog.setMax(100);
		dialog.incrementProgressBy(15);
		dialog.show();
		new Thread(new Runnable() {
			boolean flag = true;

			@Override
			public void run() {
				while (flag) {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					if (dialog.getProgress() >= dialog.getMax()) {
						Message message = new Message();
						message.obj = dialog;
						handler.sendMessage(message);
						flag = false;
					} else
						dialog.incrementProgressBy(10);
				}
			}

		}).start();
	}

}
